<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class MiuiController extends Controller
{
    public function index()
    {
    	$list = DB::table('miui')->get();
    	return view('admin.miui.index',['list'=>$list]);
    }
    public function destroy($id)
    {
    	DB::table('miui')->where('id',$id)->delete();
    	return redirect('admin/miui');
    }
    public function create()
    {
    	return view('admin.miui.add');
    }
    public function store(Request $request)
    {
    	// dd($request);
    	if ($request->hasFile('maxpic') && $request->hasFile('minpic')) {
            if ($request->file('maxpic')->isValid()) {
                $file = $request->file('maxpic');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $maxpic = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$maxpic);
            if ($request->file('minpic')->isValid()) {
               	$file = $request->file('minpic');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $minpic = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$minpic);
            }
            	$intro = $request->input('intro');
            	$links = $request->input('links');
            	$url = $request->input('url');
                $da = array('maxpic'=>$maxpic,'minpic'=>$minpic,'intro'=>$intro,'links'=>$links,'url'=>$url);
                $data = array_filter($da);
                $m = DB::table('miui')->insertGetId($data);
                if ($m>0) {
                	return redirect('admin/miui');
                }else{
        			return back()->with("msg","图片添加失败,请重新添加");
        		}
            }
        }else{
        	return back()->with("msg","图片添加失败,请重新添加");
        }
    }
    public function edit($id)
    {
    	$list = DB::table('miui')->where('id',$id)->first();
    	return view('admin.miui.edit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
    	// dd($request);
    	if ($request->hasFile('maxpic') && $request->hasFile('minpic')) {
            if ($request->file('maxpic')->isValid()) {
                $file = $request->file('maxpic');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $maxpic = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$maxpic);
            if ($request->file('minpic')->isValid()) {
               	$file = $request->file('minpic');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $minpic = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$minpic);
            }
            	$intro = $request->input('intro');
            	$links = $request->input('links');
            	$url = $request->input('url');
                $da = array('maxpic'=>$maxpic,'minpic'=>$minpic,'intro'=>$intro,'links'=>$links,'url'=>$url);
                $data = array_filter($da);
                $m = DB::table('miui')->where('id',$id)->update($data);
                if ($m>0) {
                	return redirect('admin/miui');
                }else{
        			return redirect('admin/miui');
        		}
            }
        }else{
        	return back()->with("msg","图片修改失败,请重新修改,注意选择图片");
        }
    }
}
